package pcb.views.sharedAssets.menu
{
	import flash.display.MovieClip;
	import flash.events.EventDispatcher;
	import pcb.views.uievents.TreeNodeEvent;
	
	/**
	 * ...
	 * @author ...
	 */
	public class TreeRoot extends EventDispatcher
	{
		private var _children:Vector.<TreeNode>;
		private var _singleChoice:Boolean = true;
		private var currentSelectedNode:TreeNode;		
		
		public function TreeRoot(data:XML, target:MovieClip) 
		{	
			_children = new Vector.<TreeNode>
			
			for each(var node:XML in data.children()) {
				var childNode:TreeNode = new TreeNode(target, this, node.@label, node.@id, 0, node.@type, _children.length, node.children());
				childNode.addEventListener(TreeNodeEvent.CLICKED, onClicked);
				_children.push(childNode);
			}
			draw(null, _singleChoice);
		}
		
		private function onClicked(e:TreeNodeEvent):void 
		{
			//trace("clicked " + e.selectedNode.label);
			if (e.selectedNode.nodeLevel == TreeNode.LEVEL_MODULE && e.selectedNode.hasChildren()) {
				//trace("module selected");
			} else if (e.selectedNode.nodeLevel == TreeNode.LEVEL_SECTION && e.selectedNode.hasChildren()) {
				//trace("section selected");
			} else {
				currentSelectedNode = e.selectedNode;
				//trace("lesson selected");
			}
			
			dispatchEvent(e);
			draw(e.selectedNode, _singleChoice);
		}
		
		// used to get init y pos
		public function getHeight():Number
		{
			return 0;
		}
		
		public function getPrevChildYPos(childIndex:int):Number {			
			if (childIndex > 0) {
				return _children[childIndex - 1].nodeEndYPos();
			} else {
				return 0;
			}
		}
		
		public function getNodeOnThisLevel(selectedNode:TreeNode):TreeNode {
			if (selectedNode == null) {
				return null;
			}
			
			if (selectedNode.nodeLevel == nodeLevel) {
				return selectedNode;
			} else if (selectedNode.parent.nodeLevel == nodeLevel) {
				return selectedNode.parent;
			} else {
				return selectedNode.parent.parent;
			}
		}
		
		public function draw(selectedNode:TreeNode, closeOthers:Boolean):void 
		{
			var openNode:TreeNode = getNodeOnThisLevel(selectedNode);
			
			for each (var childNode:TreeNode in _children) 
			{
				if (openNode != null && closeOthers) {
					if (openNode != childNode) {
						childNode.nodeState = TreeNode.CLOSED_NODE;
					}
				}
				childNode.removeNode();
			}
			
			for each (var childNode1 in _children) 
			{
				childNode1.draw(selectedNode, closeOthers);
			}
			
			
		}
		
		public function get nodeState():String { return TreeNode.OPEN_NODE; }
		
		public function get nodeLevel():Number { return TreeNode.LEVEL_MODULE; }
		
		public function get parent():TreeNode { return null; }
		
		public function set enabled(value:Boolean):void { 
			for each (var childNode:TreeNode in _children) 
			{
				childNode.enabled = value;
			}
		}
		
		
		/*NAV */
		
		public function selectNextNode():void 
		{
			if (currentSelectedNode == null) {
				//select first
				//_children[0].firstChild.firstChild.select();
				_children[0].select();
			} else if (currentSelectedNode.nodeLevel == TreeNode.LEVEL_MODULE) {				
				_children[1].firstChild.firstChild.select();
			} else {
				if (currentSelectedNode.parent.hasNextNode(currentSelectedNode)) {
					currentSelectedNode.parent.getNextNode(currentSelectedNode).select();
				} else {					
					
					var currMod:TreeNode = currentSelectedNode.parent.parent;
					var currSect:TreeNode = currentSelectedNode.parent;
					
					//if not last section in mod
					if (currMod.hasNextNode(currSect)) {						
						var nextSect:TreeNode = currMod.getNextNode(currSect);
						if (nextSect == null) {
							trace("NULL for some reason");
						} else {
							//select first of next section
							nextSect.firstChild.select();
						}
					} else {						
						if (currMod.index < _children.length - 1) {
							_children[currMod.index + 1].firstChild.firstChild.select();
						} else {
							trace("end of course");
						}
					}					
				}
			}
			
		}
		
		public function selectPrevNode():void 
		{
			if (currentSelectedNode == null || currentSelectedNode.nodeLevel == TreeNode.LEVEL_MODULE) {
				//select first
				trace("cant go back from nothing");
			} else if(currentSelectedNode.parent.parent.index == 1 && currentSelectedNode.parent.index == 0 && currentSelectedNode.index == 0){
				_children[0].select();
			} else {
				if (currentSelectedNode.parent.hasPrevNode(currentSelectedNode)) {					
					currentSelectedNode.parent.getPrevNode(currentSelectedNode).select();
				} else {					
					
					var currMod:TreeNode = currentSelectedNode.parent.parent;
					var currSect:TreeNode = currentSelectedNode.parent;
					
					//if not last section in mod
					if (currMod.hasPrevNode(currSect)) {						
						var prevSect:TreeNode = currMod.getPrevNode(currSect);
						if (prevSect == null) {
							trace("NULL for some reason");
						} else {
							//select first of next section
							prevSect.lastChild.select();
						}
					} else {						
						if (currMod.index > 0) {
							_children[currMod.index - 1].lastChild.lastChild.select();
						} else {
							trace("begining of course");
						}
					}					
				}
			}
		}
	}
}